/*
 * @brief
 * @author li yong (DogLee)
 * @email 20162439@qq.com
 * @version 1.0
 * @date 2018-08-02 11:37:49
 */
#ifndef DEBUG_H
#define DEBUG_H

#include <QDebug>

/* color palette
 *
 *          font               background
 *
 *          30: black          [*] 40
 *      [*] 31: red                41
 *      [*] 32: green              42
 *      [*] 33: yellow             43
 *          34: blue               44
 *      [*] 35: pink               45
 *      [*] 36: dark green         46
 *      [*] 37: white              47
 */

#define ESC_START       "\033["
#define ESC_END         "\033[0m"
#define COLOR_FATAL     "31;40;1m"
#define COLOR_ERROR     "31;40;2m"
#define COLOR_WARN      "33;40;2m"
#define COLOR_NOTICE    "36;40;2m"
#define COLOR_INFO      "32;40;2m"
#define COLOR_DEBUG     "37;40;2m"

#define S_BLACK         30
#define S_RED           31
#define S_GREEN         32
#define S_YELLOW        33
#define S_BLUE          34
#define S_PINK          35
#define S_CYAN          36
#define S_WHITE         37

#define SETCOLOR(color)   printf("\033[0;"#color"m")
#define RESETCOLOR()      printf("\033[0m")
#define MOVETOXY(x,y)     printf("\033["#x";"#y"H")

#define PRR printf("\n");

#define PP    { SETCOLOR(33);printf("%s() ", __func__); \
                SETCOLOR(37);printf("%-10s:%-5d ", __FILE__, __LINE__); \
              }


//while building for debug, qt will define this micro.
//while building for release, qt will undefine this micro.
#ifndef QT_NO_DEBUG
//print out a integer variable
#define PD(X)   do \
                { \
                    SETCOLOR(32); \
                    printf(#X" = %-5d ",(X)); \
                    PP; \
                    PRR; \
                    RESETCOLOR(); \
                    fflush(stdout); \
                } while (false);

//print out a char string variable
#define PS(X)   do \
                { \
                    SETCOLOR(31); \
                    printf("Str is < "); \
                    SETCOLOR(35); \
                    printf(X); \
                    SETCOLOR(31); \
                    printf(" > "); \
                    PP; \
                    PRR; \
                    RESETCOLOR(); \
                    fflush(stdout); \
                } while (false);

//print out a float variable
#define PF(X)   do \
                { \
                    SETCOLOR(31); \
                    printf(#X" = %-9f ",(X)); \
                    PP; \
                    PRR; \
                    RESETCOLOR(); \
                    fflush(stdout); \
                } while (false);


//print out a qt built-in class variable
#define qLog(content) qDebug() << #content << "=" << content;
#else
#define PD(X)
#define PF(X)
#define PS(X)
#define qLog(X)
#endif

typedef struct
{
    QtMsgType type;     //消息类型
    QString time;       //消息时间
    QString context;    //消息上下文信息
    QString content;    //消息内容
}DebugMessage;

/**
 * @brief myMessageOutput this function used for handle qdebug message.
 * @param type
 * @param context
 * @param msg
 */
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);

void qInstallSigactionHandler(QString app);

QString hexData(const unsigned char* pData, int len);

QString toString(const quint64 tm);

QString toString(const QDateTime &dateTime);

void PrintData(const unsigned char* pData, int len, QString cap);

void PrintData(const char* pData, int len, QString cap);

void PrintData(const QByteArray& data, QString cap);

#endif // DEBUG_H
